package nero.yncc.com.createart.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import org.xutils.view.annotation.ViewInject;

import java.util.Timer;
import java.util.TimerTask;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.bean.UserBean;
import nero.yncc.com.createart.custom.ListVIewScroll;
import nero.yncc.com.createart.utils.UserDataUtil;

import static android.view.View.GONE;

/**
 * Created by nero on 17-6-22.
 */

public class BaseFragment extends Fragment implements View.OnClickListener {
    @ViewInject(R.id.ll_root)
    LinearLayout ll_root;  //用于自动顶出任务栏 没有设置的时候为空 如果需要自动顶出 请给跟布局设置该id

    protected UserBean userBean;
    protected Context mContext;
    protected  ListVIewScroll scrollView;


    protected void initAll() {
        mContext = getContext();
        userBean= UserDataUtil.getUserData(getContext());
        initView();
        initData();
        initListener();
        getHttpData();
    }

    protected void initView() {

    }

    protected void initData() {

    }

    protected void getHttpData() {

    }
    protected void getData() {

    }
    protected void initListener() {

    }

    @Override
    public void onClick(View view) {
        doClick(view.getId());
    }

    protected void doClick(int id) {

    }


    /**
     * 获取状态栏高度 主要用于沉浸式自动顶出高度padding 在不设置颜色的时候需要
     */
    protected int getStateBarHeight() {
        int statusBarHeight1 = -1;
//获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight1;
    }

    ProgressDialog progressDialog;

    /**
     * 展示进度条
     */
    protected void showProgress() {
        if (progressDialog == null)
            progressDialog = new ProgressDialog(mContext);
        if (progressDialog.isShowing()) return;
        progressDialog.setMessage("正在加载。。。");
        progressDialog.show();
    }

    /**
     * 隐藏进度条
     */
    protected void hideProgress() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    /**
     * 顶出状态栏 需要设置跟布局ID为ll——root
     */
    protected void ding() {
//        ll_root.setPadding(0, getStateBarHeight(), 0, 0);
    }

    protected void openActivity(Class className, Bundle bundle) {
        Intent intent = new Intent(getContext(), className);
        if (bundle != null)
            intent.putExtras(bundle);
        getContext().startActivity(intent);
    }

   private ViewGroup view_root;
    private View viewRefresh;
    /**
     * 是否需要被scrollview包围 调用此方法后将自动给当前窗体所有增加一个scrollview
     * 如果需要将标题栏排除在外面 传false
     *
     * @param containTitle 是否包含标题栏
     */
    protected View byScrollRefresh(View viewRoot,boolean containTitle) {



        view_root= viewRoot.findViewById(R.id.ll_root);
        scrollView = new ListVIewScroll(getContext());
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(-1, -1);
        scrollView.setLayoutParams(params);

        viewRefresh= LinearLayout.inflate(getContext(), R.layout.refresh_top, null);
        viewRefresh.setPadding(0,getStateBarHeight(),0,0);
        ImageView iv_image=viewRefresh.findViewById(R.id.iv_image);
        Glide.with(this).load(R.mipmap.a888).into(iv_image);
        viewRefresh.setLayoutParams(new LinearLayout.LayoutParams(-1,-1));

        LinearLayout ll_view_root_2=new LinearLayout(getContext());
        ll_view_root_2.setOrientation(LinearLayout.VERTICAL);

        ll_view_root_2.setLayoutParams(params);
        ll_view_root_2.addView(viewRefresh);
        ll_view_root_2.addView(view_root);
        ll_view_root_2.setBackgroundResource(R.color.white);
        view_root.setVisibility(View.INVISIBLE);

        scrollView.addView(ll_view_root_2);
        flushAlign();

        return scrollView;

    }

    /**
     * 如果gethttp调用失败的话 检测并调用再次尝试  每次间隔10秒
     * */
    private void flushAlign(){
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                if (!isOver){
//                    if( getActivity()!=null){
//                        getActivity().runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                getHttpData();
//                                flushAlign();
//                            }
//                        });
//                    }
//
//                }
//            }
//        },10000);

    }

    boolean isOver=false;
    /**
     * 数据加载完成请调用此函数 用于展现结果 在调用了byScrollRefresh 之后。。
     * */
    protected void loadOverShow(){
        view_root.setVisibility(View.VISIBLE);
        viewRefresh.setVisibility(GONE);
        isOver=true;
    }

    protected void showToast(String msg){
        Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
    }

}
